package com.woniu.easyuidemo.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.easyuidemo.bean.Dept;
import com.woniu.easyuidemo.bean.Emp;
import com.woniu.easyuidemo.dao.DeptMapper;
import com.woniu.easyuidemo.dao.EmpMapper;
import com.woniu.easyuidemo.vo.EmpVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class EmpController {
    @Resource
    private EmpMapper empService;

    @Resource
    private DeptMapper deptService;

    @RequestMapping("/empList")
    public String listEmps(@RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize, EmpVo vo, Model model) {
        List<Emp> empList = empService.listEmps(currentPage, pageSize, vo);
        List<Dept> depts = deptService.listDepts();
        PageInfo<Emp> pageInfo = new PageInfo<>(empList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("depts", depts);
        model.addAttribute("vo", vo);
        System.out.println(vo);
        return "empList";
    }

    @RequestMapping("/editEmp/{empno}")
    public String editEmp(@PathVariable("empno") Integer empno, Model model) {
        Emp emp = empService.selectByPrimaryKey(empno);
        model.addAttribute("emp", emp);
        List<Dept> depts = deptService.listDepts();
        model.addAttribute("depts", depts);
        return "editEmp";
    }

    @RequestMapping("/update")
    public String updateEmp(Emp emp) {
        System.out.println(emp);
        int result = empService.updateByPrimaryKeySelective(emp);
        if (result != 0) {
            return "empList";
        }
        return "forward:/editEmp/" + emp.getEmpno();

    }

    @RequestMapping("/easy")
    public String test() {
        return "easyList";
    }


}
